package com.zjhcsoft.shop.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.zjhcsoft.shop.dao.HistoryDao;
import com.zjhcsoft.shop.dao.ProductDao;
import com.zjhcsoft.shop.model.History;
import com.zjhcsoft.shop.model.Product;
import com.zjhcsoft.shop.service.HistoryService;
import com.zjhcsoft.shop.service.ProductService;
import com.zjhcsoft.shop.sys.constant.Constants;
import com.zjhcsoft.util.Base64;
import com.zjhcsoft.util.CommonUtil;

@Service
public class HistoryServiceImpl implements HistoryService  {
	
	private static final Logger logger = Logger.getLogger(HistoryServiceImpl.class);
	

	/**
	 * 操作浏览记录
	 * @param product
	 * @return
	 */
	public List<History> getHistoryList(Product product){
		//获取浏览器中的cookie
		HttpServletRequest request = ServletActionContext.getRequest();
		Cookie[] cookies = request.getCookies();
		// 以下操作判断用户是否有用户登录的cookie信息
		String cookieValueBase64 = "";
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(Constants.PRODUCT_KEY)) {
					cookieValueBase64 = cookie.getValue();
					break;
				}
			}
		}
		//解码，获得cookieValue
		String cookieValue="";
		if(!"".equals(cookieValueBase64)){
			try {
				cookieValue=new String(Base64.decode(cookieValueBase64), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}
		//分解cookievalue，获取浏览记录
		String[] hisStrList=null;
		List<History> historys = new ArrayList<History>();
		List<String> newcookielist=new ArrayList<String>();
		if(!"".equals(cookieValue)){
			hisStrList = cookieValue.split(Constants.RECORD_SEPARATOR);
			//默认展现前5条
			for(int i=0;i<hisStrList.length && i<5;i++){
				String hisStr=hisStrList[i];
				String[] list = hisStr.split(Constants.WORD_SEPARATOR);
				//5个字段分别记录cookie信息
				if(list!=null && list.length==5){
					//如果浏览器cookie中已有浏览位置，则记录位置
					if(!list[0].equals(product.getId().toString())){
						newcookielist.add(hisStr);
					}
					History history = new History();
					history.setId(new Long(list[0]));
					history.setUrl("product.htm?id="+list[0]);
					history.setPtitlie(list[1]);
					history.setSmallpic(list[2]);
					history.setBigpic(list[3]);
					history.setBrowseTime(list[4]);
					historys.add(history);
				}
			}
		}
		//保存当前的浏览记录进cookie
		String newCookieValue=product.getId()+Constants.WORD_SEPARATOR
							+product.getTitle()+Constants.WORD_SEPARATOR
							+CommonUtil.jsonStr2First(product.getSmallpic())+Constants.WORD_SEPARATOR
							+CommonUtil.jsonStr2First(product.getBigpic())+Constants.WORD_SEPARATOR
							+CommonUtil.getNowTimeString("yyyy-MM-dd HH:mm:ss");
		//如果浏览记录已经保存该记录，则过滤原来的
		for(int j=0;j<newcookielist.size();j++){
				newCookieValue+=Constants.RECORD_SEPARATOR+newcookielist.get(j);
		}
		String newCookieValueBase64="";
		try {
			newCookieValueBase64 = Base64.encode(newCookieValue.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 开始保存Cookie
		Cookie cookie = new Cookie(Constants.PRODUCT_KEY, newCookieValueBase64);
		//设置有效期为一年
		cookie.setMaxAge(60*60*24*365);
		cookie.setPath("/");
		ServletActionContext.getResponse().addCookie(cookie);
		return historys;
	}
	
}